package com.hzhc.controller.wxapp;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.SysUserSecurity;
import com.hzhc.entity.SysWxMenu;
import com.hzhc.entity.UserMember;
import com.hzhc.enumBean.LoginUserType;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysUserSecurityService;
import com.hzhc.service.UserMemberService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.StringUtils;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.UserMemberVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/app")
public class WxApiLoginController extends BaseController {

    private final UserMemberService memberService;

    private final SysUserSecurityService sysUserSecurityService;

    /**
     * 微信用户登入
     *
     * @return
     */
    @PostMapping("/loginWeChatMember")
    @SysTemLog(operModul = "微信用户登陆", operType = "login", operDesc = "微信用户登陆", modular = "APP")
    public ResultUtil<UserMemberVo> loginWeChatMember(HttpServletRequest request, @RequestBody String json) {
        JSONObject object = JSONObject.parseObject(json);
        String Loginname = object.getString("loginname");
        String password = object.getString("password");
        if (StrUtil.isBlank(Loginname)) {
            return ResultUtil.build(ResultUtilCodeEnum.USERNAME_NULL);
        }
        if (StrUtil.isBlank(password)) {
            return ResultUtil.build(ResultUtilCodeEnum.PASSWORD_ERROR);
        }
        UserMember userMember = memberService.getUserMember(Loginname, password,null);
        //处理 锁相关
        String msg=lockLogin(Loginname, userMember);
        if(StringUtils.isNotEmpty(msg)){
            List<SysUserSecurity> securities = sysUserSecurityService.getBaseMapper()
                    .selectList(new QueryWrapper<SysUserSecurity>().eq("status", 1).eq("disable",0));
            UserMemberVo vo = new UserMemberVo();
            if(msg.indexOf("账号已锁")!=-1){
                vo.setShowMsg("账号已锁定,"+securities.get(0).getPasswordLockTime()+"分钟后再试");
            }else if(msg.indexOf("剩余")!=-1){//表示待锁账号
                vo.setShowMsg("用户名密码错误"+securities.get(0).getPasswordErrorNum()+"次,账号锁定"+securities.get(0).getPasswordLockTime()+"分钟");
            }
            return  ResultUtil.build(vo,400,msg);
        }
        if (userMember != null) {
            String token = TokenUtil.signMemberToken(userMember);
            UserMemberVo vo = new UserMemberVo();
            BeanUtils.copyProperties(userMember, vo);
            vo.setToken(token);
            vo.setUserType(LoginUserType.MEMBER.getCode());
            //保存登录时间
            userMember.setLastLoginTime(LocalDateTime.now());
            memberService.updateById(userMember);
            return ResultUtil.build(vo, ResultUtilCodeEnum.LOGIN_SUCCESS);
        }
        return ResultUtil.build(ResultUtilCodeEnum.ACCOUNT_PASSWORD_ERROR);
    }

    private String lockLogin(String loginname, UserMember userMember) {
        if (userMember != null) {
            if(userMember.getLoginTime()!=null && userMember.getLoginTime().isAfter(LocalDateTime.now())){
                return "账号已锁!";
            }
            userMember.setLoginNum(0);
            userMember.setLoginTime(null);
            memberService.updateById(userMember);
            return "";
        }else{
            String msg=sysUserSecurityService.checkLoginFailure(loginname,"app",null);
            return msg;
        }
    }

    /**
     * 菜单
     */
    @RequestMapping("/syswxMenuTree")
    public List<SysWxMenu> syswxMenuTree(@RequestBody String json) {
        JSONObject object = JSONObject.parseObject(json);
        Long memberId = object.getLong("memberId");
        String title = object.getString("title");
        return memberService.getSyswxMenuTree(memberId, "",title);
    }

}
